package com.jctrip.hotel.source.xxl.atour.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.atour.AtourService;
import com.jctrip.hotel.source.service.atour.AtourTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-04 20:44
 * @Description:
 */
@Slf4j
public class SyncAtourLongerRatesTaskThread implements Runnable {
    private final Integer appId;

    private final String key;
    private final Integer mebId;
    private final Integer days;
    private final Integer addDays;

    public SyncAtourLongerRatesTaskThread(Integer appId, String key, Integer mebId,
                                          Integer addDays, Integer days) {
        this.appId = appId;
        this.key = key;
        this.mebId = mebId;
        this.days = days;
        this.addDays = addDays;
    }

    @Override
    public void run() {
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
        AtourService atourService = BeanContext.getApplicationContext().getBean(AtourService.class);
        AtourTaskService atourTaskService = BeanContext.getApplicationContext().getBean(AtourTaskService.class);
        try {
            while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_ATOUR_LONGER_RATES_LIST) > 0) {
                String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_ATOUR_LONGER_RATES_LIST);
                if (StringUtils.isBlank(hotelId)) {
                    continue;
                }
                Integer atourHotelId = atourService.parseHotelId(hotelId);
                Date start = DateUtils.addDays(new Date(), addDays);
                Date end = DateUtils.addDays(start, days);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                atourTaskService.syncAtourRatesTaskPop(appId, key, atourHotelId, simpleDateFormat.format(start),
                        simpleDateFormat.format(end), mebId);
            }
        } catch (Exception e) {
            log.error("SyncAtourRatesTaskThread error", e);
        }

    }
}